Java 8 Interview Questions and Answers
Question: How lambda expression and functional interfaces are related?Answer:Lambda expressions can only be applied to abstract method of functional interface. For example: Runnable has only one abstract method called run, so it can be used as below: // Using lambda expression Thread t1=new Thread( ()->System.out.println("In Run method") ); Here we are using Thread constructor which takes Runnable as parameter. As you can see we did not specify any function name here, as Runnable has only one abstract method, java will implicitly create anonymous Runnable and execute run method. It will be as good as below code. Thread t1=new Thread(new Runnable() { @Override public void run() { System.out.println("In Run method"); } }); |
Is it helpful?
Yes
No
Most helpful rated by users:
- What are new features which got introduced in Java 8?
- What are main advantages of using Java 8?
- Can you explain the syntax of Lambda expression?
- What are functional interfaces?
- What Is a Default Method and When Do We Use It?